package com.bigdata.spark.testapp

import java.util.{Properties, Random}

import com.alibaba.fastjson.JSONObject
import com.bigdata.spark.testapp.bean.activebean
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}

import scala.collection.mutable.ListBuffer

object MyData_money {

    def main(args: Array[String]): Unit = {

        // 生成模拟数据
        // 格式 ：timestamp area city userid adid
        // 含义： 时间戳   区域  城市 用户 广告

        // Application => Kafka => SparkStreaming => Analysis
        val prop = new Properties()
        // 添加配置
        prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092")
        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")

        val producer = new KafkaProducer[String, String](prop)  //添加kafkaProducer对象

        while ( true ) {
            mockdata().foreach(
                data => {
                    // 向Kafka中生成数据
                    val record = new ProducerRecord[String, String]("payment", data)
                    producer.send(record)
                    println(data)
                }
            )

            Thread.sleep(2000)
        }

    }
    def mockdata() = {


        val jsonObject: JSONObject = new JSONObject()

        val list = ListBuffer[String]()
        val areaList = ListBuffer[String]("华北", "华东", "华南")
        val cityList = ListBuffer[String]("北京", "上海", "深圳")
        val sexList = ListBuffer[String]("男", "女", "无")

        val activebean1 = new activebean


        for ( i <- 1 to new Random().nextInt(10) ) {

            val area = areaList(new Random().nextInt(3))
            val city = cityList(new Random().nextInt(3))

            var userid = new Random().nextInt(6) + 1
            var username = new Random().nextInt(6) + 1
            var loca = cityList(new Random().nextInt(3))
            var shopid = new Random().nextInt(6) + 1




             var jstr = "{\"start\":{" +
                            "\"user_id\""+":\"" + userid + '\"'+
                            ",\"username\"" +":\""+ username + '\"' +
                            ",\"sex\"" + ":\""+shopid + '\"'+
                            ",\"phone\"" +":\""+ loca + '\"' +
                            ",\"ts\"" +":"+ System.currentTimeMillis() +
                            '}'+'}'


            list.append(jstr)
        }

        list
    }

}
